package com.hskn.hss.module.tlequipmodel.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hskn.hss.core.annotation.DataScope;
import com.hskn.hss.core.domain.BaseEntity;
import com.hskn.hss.core.utils.LoginUtils;
import com.hskn.hss.entity.AjaxResult;
import com.hskn.hss.entity.ListEntity;
import com.hskn.hss.exception.HssException;
import com.hskn.hss.module.tlequipmodel.entity.TlEquipmodel;
import com.hskn.hss.module.tlequipmodel.mapper.TlEquipmodelMapper;
import com.hskn.hss.module.tlequipmodel.service.ITlEquipmodelService;
import com.hskn.hss.module.userdepart.entity.SysUserDepart;
import com.hskn.hss.module.userdepart.mapper.SysUserDepartMapper;
import com.hskn.hss.utils.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hskn
 * @since 2021-05-11
 */
@Service
public class TlEquipmodelServiceImpl extends ServiceImpl<TlEquipmodelMapper, TlEquipmodel> implements ITlEquipmodelService {

    @Resource
    TlEquipmodelMapper tlEquipmodelMapper;
    @Resource
    SysUserDepartMapper sysUserDepartMapper;

    @Override
    public AjaxResult insert(TlEquipmodel tlEquipmodel) throws HssException {

        String uid = LoginUtils.getUser().getUid();
        QueryWrapper<SysUserDepart> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysUserDepart::getUserid, uid);
        String deptid = sysUserDepartMapper.selectOne(queryWrapper).getDepid();

        tlEquipmodel.setCreateby(uid);
        tlEquipmodel.setCreatetime(new Date());
        tlEquipmodel.setDeptid(deptid);
        tlEquipmodel.setStatus("1");
        int i = tlEquipmodelMapper.insert(tlEquipmodel);
        boolean flag = i > 0 ? true : false;

        if (flag) {
            return AjaxResult.success("操作成功");
        } else {
            return AjaxResult.fail("操作失败，请稍后再试");
        }
    }

    @Override
    public AjaxResult update(JSONObject params) throws HssException {

        String uid = LoginUtils.getUser().getUid();
        TlEquipmodel tlEquipmodel = params.getObject("data", TlEquipmodel.class);
        tlEquipmodel.setUpdateby(uid);
        tlEquipmodel.setUpdatetime(new Date());
        int i = tlEquipmodelMapper.updateById(tlEquipmodel);
        boolean flag = i > 0 ? true : false;
        if (flag) {
            return AjaxResult.success("操作成功");
        } else {
            return AjaxResult.fail("操作失败，请稍后再试");
        }
    }

    @Override
    public AjaxResult updatebyids(TlEquipmodel tlEquipmodel) throws HssException {

        String uid = LoginUtils.getUser().getUid();
        String ids = tlEquipmodel.getIds();
        String[] idArray = ids.split(",");
        tlEquipmodel.setUpdateby(uid);
        tlEquipmodel.setUpdatetime(new Date());

        UpdateWrapper<TlEquipmodel> tlEquipmodelUpdateWrapper = new UpdateWrapper<>();
        tlEquipmodelUpdateWrapper.lambda().in(TlEquipmodel::getId, idArray);

        tlEquipmodelMapper.update(tlEquipmodel,tlEquipmodelUpdateWrapper);
        return AjaxResult.success("操作成功");
    }

    @Override
    public AjaxResult deletebyid(JSONObject params) throws HssException {
        String id = params.getString("id");
        tlEquipmodelMapper.deleteById(id);
        return AjaxResult.success("操作成功");
    }
    @Override
    public AjaxResult deletebatchbyid(TlEquipmodel tlEquipmodel) throws HssException {
        String ids = tlEquipmodel.getIds();
        String[] idArray = ids.split(",");
        tlEquipmodelMapper.deleteBatchIds(Arrays.asList(idArray));
        return AjaxResult.success("操作成功");
    }
    @Override
    public TlEquipmodel getDetail(JSONObject params) throws HssException {
        String id = params.getString("id");
        TlEquipmodel tlEquipmodel = tlEquipmodelMapper.selectById(id);
        return tlEquipmodel;
    }

    @Override
    public List<ListEntity> getSelectList(JSONObject params) {
        Map<String, Object> map = new HashMap();
        map.put("equiptypeid", params.getString("equiptypeid"));
        map.put("manufacturerid", params.getString("manufacturerid"));
        List<ListEntity> resultList = tlEquipmodelMapper.getSelectList(map);
        return resultList;
    }

    @Override
    @DataScope(deptAlias = "deptid", userAlias = "createby, updateby")
    public JSONObject getList(BaseEntity entity, JSONObject params) throws HssException {
        Long page = StringUtils.isEmpty(params.getLong("page")) ? 1 : params.getLong("page");
        Long size = StringUtils.isEmpty(params.getLong("size")) ? 10 : params.getLong("size");
        JSONObject json = new JSONObject();

        Map<String, Object> map = new HashMap();
        map.put("equipmodel", params.getString("equipmodel"));
        map.put("equiptypeid", params.getString("equiptypeid"));
        map.put("manufacturerid", params.getString("manufacturerid"));
        map.put("status", params.getString("status"));
        map.put("params", entity.getParams());
        Page<TlEquipmodel> listPage = new Page<>(page, size, true);
        IPage<TlEquipmodel> iPage = tlEquipmodelMapper.listEquipmodel(listPage, map);
        json.put("total", iPage.getTotal());
        json.put("size", iPage.getSize());
        json.put("pages", iPage.getPages());
        json.put("current", iPage.getCurrent());
        json.put("equipmodellist", iPage.getRecords());
        return json;
    }
}
